(ns WebPageDownloader
  (:require [net.cgrand.enlive-html :as html])
  (:require [clojure.string :as string])
  ;;(:require [CurrentBusinessLogic :as cbl])
  (:use CurrentBusinessLogic)
  )


(def serbia-url "http://www.belex.rs/trzista_i_hartije/hartije/akcije/sve")

(defn get-page
  "Gets the html page from passed url"
  [url]
  (html/html-resource (java.net.URL. url)))

(defn h3+table
  "Return seq of <h3> and table tags, where content of the <h3> tag meet defined condition"
  [url]
  (html/select (get-page url) 
              {[:h3] [:table.tabela.bilans]}                
               ))

(defn get-data-balance_sheet
  "Gets data only from most recent balance sheet, the active side"
  [table-source]
  (let [[table-nodes] (html/select table-source [:td])
       [headers] (html/select table-source [:h3])]
     (when (and (re-seq #"\baktiva – izmena\b.*"
                          (apply str (:content headers)))
                  (>= (count table-nodes) 3))
         table-source)))

(defn get-data-income_satement
  "Gets data only from most recent balance sheet"
  [table-source]
  (let [[table-nodes] (html/select table-source [:td])
        [headers] (html/select table-source [:h3])]
    (when (and 
             (re-seq #"\buspeha – izmena\b.*" (apply str (:content headers)))
             (>= (count table-nodes) 3)) 
       table-source)))


(defn get-data-balance_sheet_pasiva
  [table-source]
  (let [[table-nodes] (html/select table-source [:td])
        [headers] (html/select table-source [:h3])]
    (when (and 
             (re-seq #"\bpasiva – izmena\b.*" (apply str (:content headers)))
             (>= (count table-nodes) 3)) 
       table-source)))

(defn parse-integer_balance_sheet
  [url row]
  (map #(-> (first %1) (string/replace "." "") (Integer/parseInt)) 
       (seq (map :content (flatten (map #(html/select % [[:tr (html/nth-of-type row)] :td])
                                                                                  (remove nil? (map get-data-balance_sheet 
                                                                                                     (h3+table url)))))))))

(defn parse-integer_balance_sheet_pasiva
  [url row]
  (map #(-> (first %1) (string/replace "." "") (Integer/parseInt)) 
       (seq (map :content (flatten (map #(html/select % [[:tr (html/nth-of-type row)] :td])
                                                                                  (remove nil? (map get-data-balance_sheet_pasiva 
                                                                                                     (h3+table url)))))))))
(defn parse-integer_income_statement
  [url row]
  (map #(-> (first %1) (string/replace "." "") (Integer/parseInt)) 
       (seq (map :content (flatten (map #(html/select % [[:tr (html/nth-of-type row)] :td])
                                                                                  (remove nil? (map get-data-income_satement 
    
                                                                                                 (h3+table url)))))))))

																								 
(defn my-test2
  [seq1 seq2]
  (let [counter 0]
    (if (= (count seq1) 0)
    (println "end")
    (do      
      (print (main_profit_ratio (first seq1) (first seq2))) 
      (print " ") 
      (recur (rest seq1) (rest seq2)))  
  )))

(defn my-test3
  [seq1 seq2 seq3]
  (let [counter 0]
    (if (= (count seq1) 0)
    (println "end")
    (do      
      (print (liquidityRatio_fast (first seq1) (first seq2) (first seq3))) 
      (print " ") 
      
      (recur (rest seq1) (rest seq2) (rest seq3)))  
  )))																								 
																								 
																								 
(defn calculate-ratio_main_profit_ratio ;;20  2
  [url]  
  (my-test2 (parse-integer_income_statement url 20) (parse-integer_balance_sheet url 2))
  )

(defn calculate-ratio_aditionalSales_ratio ;;3  2
  [url]  
  (my-test2 (parse-integer_income_statement url 3) (parse-integer_balance_sheet url 2))
  )

(defn calculate-ratio_liquidityRatio_current ;;14  18
  [url]  
  (my-test2 (parse-integer_balance_sheet url 14) (parse-integer_balance_sheet_pasiva url 18))
  )

(defn calculate-ratio_liquidityRatio_fast ;; 14 15 18
  [url]
  (my-test3 (parse-integer_balance_sheet url 14) (parse-integer_balance_sheet url 15)(parse-integer_balance_sheet_pasiva url 18))
  )

(defn print-date
  [url]  
  (for [s (remove #{"AKTIVA:"} (flatten 
    (map :content (flatten 
                    (map #(html/select % [[:tr (html/nth-of-type 1)] :td])
                                                                               (remove nil? (map get-data-balance_sheet 
                                                                                                  (h3+table url)))))))) 
         :while true] (string/replace s "\n" "\t"))
  )  
  











